home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / ScrollingPanel.java < prev    next >
Text File  |  1998-10-19  |  14KB  |  445 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Point;
  5. import java.awt.Component;
  6. import java.awt.Adjustable;
  7.  
  8. //     02/15/97    RKM    Changed add to call setComponent
  9. //                    Changed setComponent to add the component before the VBar, HBar, & cornerRect
  10. //                    Removed bOsFlag and added isSolaris to OS
  11. //                    Added handling of correct scrollbar width for the Mac
  12. //  05/08/97    CAR Changed handleEvent to check that event target is either the HBar OR the VBar
  13. //                  before executing a scroll.  This fixed the bug where a scroll event that was
  14. //                  not targeted at the VBar would cause the VBar logic to execute.
  15. //  05/11/97    CAR Changed setComponent to not call super.removeAll and addInternalComponents.
  16. //                  Replaced all calls to repaint with calls to placeComponents (except in reshape).
  17. //                  Changed visibility of placeComponents from default to private.
  18. //  05/16/97    CAR Changed scrollVerticalAbsolute and scrollHorizontalAbsolute to account for
  19. //                  the visible page size
  20. //  05/19/97    CAR Changed reshape to call placeComponents
  21. //  05/20/97    CAR Added vendor/version check to placeComponents, scrollDown/Right/PageDown/PageRight
  22. //     07/15/97    RKM    Changed call to super.setLayout so it would bypass our override
  23. //     07/19/97    RKM    Hide components immediately after creation, instead of waiting to post add (eliminate flicker)
  24. //                    Removed hard coded scrollbar size (called getPreferredSize())
  25. //                    Removed add overrides and added addImpl (this is the 1.1 way of doing this)
  26. //                    Changed removeAll to not call super, but remove the one component that could be there
  27. //  07/25/97    RKM Added getScrollPosition a la ScrollPane
  28. //  07/29/97    RKM Made bSun1_1 static
  29. //  07/30/97    CAR marked fields transient as needed
  30. //  08/21/97    CAR Ripped out guts of ScrollingPanel, now extend directly from java.awt.ScrollPane
  31. //    08/30/97    LAB    Deprecated class (Addresses Mac Bug #7651).  Updated version to 1.1.
  32. //    09/04/97    LAB    Backed out deprecation.  Interactions are more useful than baseclass.
  33.  
  34. /**
  35.  * Similar to java.awt.ScrollPane, but with better Interaction Wizard support.
  36.  * <p>
  37.  * The ScrollingPanel is a panel with automatic scroll bars.
  38.  * It may contain only one component/container/panel/etc.
  39.  * It automatically tracks the size of this (usually large) component and
  40.  * provides scroll bars so the entire component may be viewed within a
  41.  * panel of limited size.
  42.  * Typically, the added component would be a panel (or some other container)
  43.  * which would then contain a variety of other components.
  44.  *
  45.  * @see java.awt.ScrollPane
  46.  * @version 1.1, August 30, 1997
  47.  * @author Symantec
  48.  */
  49. public class ScrollingPanel extends java.awt.ScrollPane
  50. {
  51.     /**
  52.      * Constructs a default ScrollingPanel.
  53.      * The panel is initialized with a null component, zero
  54.      * minimum height and zero minimum width.
  55.      */
  56.     public ScrollingPanel()
  57.     {
  58.         super();
  59.     }
  60.  
  61.     /**
  62.      * Constructs a new ScrollingPanel initialized with the
  63.      * specified scrollbarDisplayPolicy.
  64.      * @param scrollbarDisplayPolicy policy for when scrollbars should be shown
  65.      */
  66.     public ScrollingPanel(int scrollbarDisplayPolicy)
  67.     {
  68.         super(scrollbarDisplayPolicy);
  69.     }
  70.  
  71.     /**
  72.      * Constructs a new ScrollingPanel initialized with the
  73.      * specified component, minimum height and minimum width.
  74.      * @param component the component (usually a Panel) to be
  75.      * scrolled
  76.      * @param minWidth the value to be used for the minimumSize()
  77.      * width of the ScrollingPanel
  78.      * @param minHeight the value to be used for the minimumSize()
  79.      * height of the ScrollingPanel
  80.      */
  81.     public ScrollingPanel(Component component, int minWidth, int minHeight)
  82.     {
  83.         super();
  84.         addImpl(component, null, -1);
  85.         setSize(minWidth, minHeight);
  86.     }
  87.  
  88.     /**
  89.      * Sets the value to be used for the minimumSize() width
  90.      * of the ScrollingPanel.
  91.      * @param minWidth the value to be used for the minimumSize()
  92.      * width of the ScrollingPanel
  93.      * @see #getMinimumWidth
  94.      */
  95.     public void setMinimumWidth(int minWidth)
  96.     {
  97.         this.width = minWidth;
  98.     }
  99.  
  100.     /**
  101.      * Gets the current value used for the minimumSize()
  102.      * width of the ScrollingPanel.
  103.      * @return the current minimum width value
  104.      */
  105.     public int getMinimumWidth()
  106.     {
  107.         return this.width;
  108.     }
  109.  
  110.     /**
  111.      * Sets the value to be used for the minimumSize()
  112.      * height of the ScrollingPanel.
  113.      * @param minHeight the value to be used for the minimumSize()
  114.      * height of the ScrollingPanel
  115.      * @see #getMinimumHeight
  116.      */
  117.     public void setMinimumHeight(int minHeight)
  118.     {
  119.         this.height = minHeight;
  120.     }
  121.  
  122.     /**
  123.      * Gets the value used for the minimumSize() height
  124.      * of the ScrollingPanel.
  125.      * @return current minimum height value
  126.      * @see #setMinimumHeight
  127.      */
  128.     public int getMinimumHeight()
  129.     {
  130.         return this.height;
  131.     }
  132.  
  133.     /**
  134.      * @deprecated
  135.      * removed functionality
  136.      */
  137.     public void setVerticalGap(int gapPixels)
  138.     {
  139.     }
  140.  
  141.     /**
  142.      * @deprecated
  143.      * removed functionality
  144.      */
  145.     public int getVerticalGap()
  146.     {
  147.         return 0;
  148.     }
  149.  
  150.     /**
  151.      * @deprecated
  152.      * removed functionality
  153.      */
  154.     public void setHorizontalGap(int gapPixels)
  155.     {
  156.     }
  157.  
  158.     /**
  159.      * @deprecated
  160.      * removed functionality
  161.      */
  162.     public int getHorizontalGap()
  163.     {
  164.         return 0;
  165.     }
  166.  
  167.     /**
  168.      * @deprecated
  169.      * replaced by constructor which takes a scrollbarDisplayPolicy
  170.      */
  171.     public void setShowVerticalScroll(boolean cond)
  172.     {
  173.     }
  174.  
  175.     /**
  176.      * @deprecated
  177.      * replaced by getScrollbarDisplayPolicy
  178.      */
  179.     public boolean getShowVerticalScroll()
  180.     {
  181.         int i = getScrollbarDisplayPolicy();
  182.         if (i == SCROLLBARS_ALWAYS || i == SCROLLBARS_AS_NEEDED)
  183.             return true;
  184.         else
  185.             return false;
  186.     }
  187.  
  188.     /**
  189.      * @deprecated
  190.      * replaced by constructor which takes a scrollbarDisplayPolicy
  191.      */
  192.     public void setShowHorizontalScroll(boolean cond)
  193.     {
  194.     }
  195.  
  196.     /**
  197.      * @deprecated
  198.      * replaced by getScrollbarDisplayPolicy
  199.      */
  200.     public boolean getShowHorizontalScroll()
  201.     {
  202.         int i = getScrollbarDisplayPolicy();
  203.         if (i == SCROLLBARS_ALWAYS || i == SCROLLBARS_AS_NEEDED)
  204.             return true;
  205.         else
  206.             return false;
  207.     }
  208.  
  209.     /**
  210.      */
  211.     public void setScrollLineIncrement(int scrollLineIncrement)
  212.     {
  213.         getHAdjustable().setUnitIncrement(scrollLineIncrement);
  214.         getVAdjustable().setUnitIncrement(scrollLineIncrement);
  215.     }
  216.  
  217.     /**
  218.      */
  219.     public int getScrollLineIncrement()
  220.     {
  221.         return Math.max(getHAdjustable().getUnitIncrement(),
  222.                         getVAdjustable().getUnitIncrement());
  223.     }
  224.  
  225.     /**
  226.      * Sets the component in this ScrollingPanel. This is the component
  227.      * that gets scrolled. The ScrollingPanel can only contain one component.
  228.      * Any previous component will be removed before the new one is added.
  229.      * @param comp the component to add
  230.      * @see #getComponent
  231.      */
  232.     public void setComponent(Component comp)
  233.     {
  234.         addImpl(comp, null, -1);
  235.     }
  236.  
  237.     /**
  238.      * Gets the current component in the ScrollingPanel.
  239.      * This is the component that gets scrolled.
  240.      * @return the current component in the ScrollingPanel
  241.      * @see #setComponent
  242.      */
  243.     public Component getComponent()
  244.     {
  245.         if (getComponentCount() > 0) {
  246.             return getComponent(0);
  247.         }
  248.         else
  249.             return null;
  250.     }
  251.  
  252.     /**
  253.      * Scrolls up by the number of pixels specified in the method setScrollLineIncrement().
  254.      * The default is one pixel.
  255.      * @see #scrollDown
  256.      * @see #scrollLeft
  257.      * @see #scrollRight
  258.      * @see #setScrollLineIncrement
  259.      * @see #getScrollLineIncrement
  260.      */
  261.     public void scrollUp()
  262.     {
  263.         Point p = getScrollPosition();
  264.         setScrollPosition(new Point(p.x, p.y - getVAdjustable().getUnitIncrement()));
  265.     }
  266.  
  267.     /**
  268.      * Scrolls left by the number of pixels specified in the method setScrollLineIncrement().
  269.      * The default is one pixel.
  270.      * @see #scrollRight
  271.      * @see #scrollUp
  272.      * @see #scrollDown
  273.      * @see #setScrollLineIncrement
  274.      * @see #getScrollLineIncrement
  275.      */
  276.     public void scrollLeft()
  277.     {
  278.         Point p = getScrollPosition();
  279.         setScrollPosition(new Point(p.x - getHAdjustable().getUnitIncrement(), p.y));
  280.     }
  281.  
  282.     /**
  283.      * Scrolls down by the number of pixels specified in the method setScrollLineIncrement().
  284.      * The default is one pixel.
  285.      * @see #scrollUp
  286.      * @see #scrollLeft
  287.      * @see #scrollRight
  288.      * @see #setScrollLineIncrement
  289.      * @see #getScrollLineIncrement
  290.      */
  291.     public void scrollDown()
  292.     {
  293.         Point p = getScrollPosition();
  294.         setScrollPosition(new Point(p.x, p.y + getVAdjustable().getUnitIncrement()));
  295.     }
  296.  
  297.     /**
  298.      * Scrolls right by the number of pixels specified in the method setScrollLineIncrement().
  299.      * The default is one pixel.
  300.      * @see #scrollLeft
  301.      * @see #scrollUp
  302.      * @see #scrollDown
  303.      * @see #setScrollLineIncrement
  304.      * @see #getScrollLineIncrement
  305.      */
  306.     public void scrollRight()
  307.     {
  308.         Point p = getScrollPosition();
  309.         setScrollPosition(new Point(p.x + getHAdjustable().getUnitIncrement(), p.y));
  310.     }
  311.  
  312.     /**
  313.      * Scrolls one "page" up.
  314.      * The page size is the size of this ScrollingPanel not including the
  315.      * scroll bar, if present.
  316.      * @see #scrollPageDown
  317.      * @see #scrollPageLeft
  318.      * @see #scrollPageRight
  319.      */
  320.     public void scrollPageUp()
  321.     {
  322.         Point p = getScrollPosition();
  323.         setScrollPosition(new Point(p.x, p.y - getVAdjustable().getBlockIncrement()));
  324.     }
  325.  
  326.     /**
  327.      * Scrolls one "page" left.
  328.      * The page size is the size of this ScrollingPanel not including the
  329.      * scroll bar, if present.
  330.      * @see #scrollPageRight
  331.      * @see #scrollPageUp
  332.      * @see #scrollPageDown
  333.      */
  334.     public void scrollPageLeft()
  335.     {
  336.         Point p = getScrollPosition();
  337.         setScrollPosition(new Point(p.x - getHAdjustable().getBlockIncrement(), p.y));
  338.     }
  339.  
  340.     /**
  341.      * Scrolls one "page" down.
  342.      * The page size is the size of this ScrollingPanel not including the
  343.      * scroll bar, if present.
  344.      * @see #scrollPageUp
  345.      * @see #scrollPageLeft
  346.      * @see #scrollPageRight
  347.      */
  348.     public void scrollPageDown()
  349.     {
  350.         Point p = getScrollPosition();
  351.         setScrollPosition(new Point(p.x, p.y + getVAdjustable().getBlockIncrement()));
  352.     }
  353.  
  354.     /**
  355.      * Scrolls one "page" right.
  356.      * The page size is the size of this ScrollingPanel not including the
  357.      * scroll bar, if present.
  358.      * @see #scrollPageLeft
  359.      * @see #scrollPageUp
  360.      * @see #scrollPageDown
  361.      */
  362.     public void scrollPageRight()
  363.     {
  364.         Point p = getScrollPosition();
  365.         setScrollPosition(new Point(p.x + getHAdjustable().getBlockIncrement(), p.y));
  366.     }
  367.  
  368.     /**
  369.      * Scrolls to an absolute vertical position.
  370.      * The contained component's given pixel position will be scrolled to the
  371.      * top line of the scrolling panel. The provided position is constrained to the range
  372.      * [0, component's height - view port height].
  373.      * @param position the pixel position to scroll to
  374.      * @see #scrollHorizontalAbsolute
  375.      */
  376.     public void scrollVerticalAbsolute(int position)
  377.     {
  378.         Point p = getScrollPosition();
  379.         setScrollPosition(new Point(p.x, position));
  380.     }
  381.  
  382.     /**
  383.      * Scrolls to an absolute horizontal position.
  384.      * The contained component's given pixel position will be scrolled to the
  385.      * leftmost part of the scrolling panel. The provided position is constrained to the range
  386.      * [0, component's width - view port width].
  387.      * @param position the pixel position to scroll to
  388.      * @see #scrollVerticalAbsolute
  389.      */
  390.     public void scrollHorizontalAbsolute(int position)
  391.     {
  392.         Point p = getScrollPosition();
  393.         setScrollPosition(new Point(position, p.y));
  394.     }
  395.  
  396.     /**
  397.      * Returns the recommended dimensions to properly display this component.
  398.      * This is a standard Java AWT method which gets called to determine
  399.      * the recommended size of this component.
  400.      *
  401.      * For each axis, it returns the larger of the current size or the
  402.      * minimum size.
  403.      *
  404.      * @see #minimumSize
  405.      */
  406.     public Dimension preferredSize()
  407.     {
  408.         Dimension s = size();
  409.         Dimension m = minimumSize();
  410.         return new Dimension(Math.max(s.width, m.width), Math.max(s.height, m.height));
  411.     }
  412.  
  413.     public Dimension getPreferredSize()
  414.     {
  415.         return preferredSize();
  416.     }
  417.  
  418.     /**
  419.      * Returns the minimum dimensions to properly display this component.
  420.      * This is a standard Java AWT method which gets called to determine
  421.      * the minimum size of this component.
  422.      *
  423.      * The value returned is set using the setMinimumHeight() and
  424.      * setMinimumWidth() methods or when this ScrollingPanel is constructed.
  425.      *
  426.      * @see #preferredSize
  427.      * @see #setMinimumHeight
  428.      * @see #setMinimumWidth
  429.      */
  430.     public Dimension minimumSize()
  431.     {
  432.         return new Dimension(width, height);
  433.     }
  434.  
  435.     public Dimension getMinimumSize()
  436.     {
  437.         return minimumSize();
  438.     }
  439.  
  440.     private int width = 100;
  441.     private int    height = 100;
  442.  
  443. }
  444.  
  445.